﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;
using TPCN.Common;
using TPCN.Commons;

namespace TPCN.Accounts
{
    public partial class Register : System.Web.UI.Page
    {
        private string m_email = String.Empty;
        private string m_password = String.Empty;
        private string m_name = String.Empty;
        private string m_phone = String.Empty;
        private string m_gender = String.Empty;
        private string m_address = String.Empty;
        private string m_active = "TRUE";

        private static KhachHangObj obj = null;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (isExist(Email.Text.ToLower().Trim()))
                {
                    lblError.Text = "Email đã được sử dụng. Vui lòng sử dụng email khác!";
                }
                else
                {
                    setValueToObject();
                    if (KhachHangDAL.Insert(obj) != 0)
                    {
                        successpanel.Visible = true;
                        registerpanel.Visible = false;
                    }
                    else
                    {
                        lblError.Text = "Đăng kí không thành công, vui lòng thử lại sau!";
                    }
                }
            }
        }

        protected void setValueToObject()
        {
            m_email = Email.Text.ToLower().Trim();
            m_password = Password.Text.ToLower().Trim();
            m_password = Security.EncryptPassword(m_password);
            m_name = txtHovaten.Text.Trim();
            obj = new KhachHangObj();
            obj.Email = m_email;
            obj.Password = m_password;
            obj.Hovaten = m_name;
            obj.Gioitinh = m_gender;
            obj.Diachi = m_address;
            obj.Active = m_active;
            obj.Sodienthoai = m_phone;
        }

        private bool isExist(string email)
        {
            KhachHangObj tmp = null;
            tmp = KhachHangDAL.GetByEmail(email);
            return (tmp.KhachID != null);
        }

        protected void Dologin(string email)
        {
            string customerID = KhachHangDAL.GetByEmail(email).KhachID;
            HttpContext.Current.Session["user"] = customerID;
            Response.Redirect("~");
        }

        protected void ContinueButton_Click(object sender, EventArgs e)
        {
            Dologin(obj.Email);
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (args.Value.Length < 6)
                args.IsValid = false;
            else
                args.IsValid = true;
        }
    }
}